﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lswweb.IDAL;
using Lswweb.Model;
using Lswweb.Domain;

namespace Lswweb.Domain
{
    public class CurrencyServices
    {
        private static ICurrencyDAL dal = DALFactory.CreateCurrencyDAL();

        public static Currency GetCurrency(int id)
        {
            if (id <= 0)
                return null;

            return dal.FindById(id);
        }

        public static Currency GetDefaultCurrency()
        {
            Currency currency=dal.FindByDefault();
            if (currency == null)
                currency = new Currency() { Name = "美元", Symbol = "$", Code = "USD", ExchangeRate = 1 };

            return currency;
        }

        public static IList<Currency> FindAll()
        {
            return dal.FindAll();
        }

        public static void Insert(Currency info)
        {
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            EntityHandler(info);

            try
            {
                dal.Add(info);
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            catch
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);

            }
        }

        public static void Update(Currency info)
        {
            if (info == null)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            EntityHandler(info);

            Currency oldEntity = dal.FindById(info.Id);
            if (string.IsNullOrEmpty(info.Icon))
            {
                if (oldEntity == null)
                {
                    SiteContext.RenderMessage(ExceptionType.UnknownError);
                    return;
                }
                info.Icon = oldEntity.Icon;
            }
            else
            {
                if (!string.IsNullOrEmpty(oldEntity.Icon))
                {
                    Globals.FileDelete(oldEntity.Icon);
                }
            }

            try
            {
                dal.Update(info);
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            catch
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);

            }
        }

        public static void Delete(int id)
        {
            if (id <= 0)
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
                return;
            }

            try
            {
                dal.Delete(id);
                SiteContext.RenderMessage(ExceptionType.HandleSuccess);
            }
            catch
            {
                SiteContext.RenderMessage(ExceptionType.UnknownError);
            }
        }

        public static string ForamtPrice(decimal price)
        {
            return FormatPrice(price, SiteContext.Current.CurrentCurrency);
        }
        public static string FormatPrice(decimal price, int rateId)
        {
            Currency rate = GetCurrency(rateId);

            return FormatPrice(price, rate);
        }
        public static string FormatPrice(decimal price, Currency rate)
        {
            return string.Format(
                "{0} {1}",
                rate.Symbol,
                rate.Exchange(price));
        }

        private static void EntityHandler(Currency currency)
        {
            if (!string.IsNullOrEmpty(currency.Icon))
            {
                currency.Icon=ImageHelper.MoveImagesToDirectory(SiteModule.Currency, currency.Icon);
            }
        }
    }
}
